package eu.dominicum.ra.db4o.outbound;

import javax.resource.spi.ConnectionRequestInfo;

/**
 * This class implements the ConnectionRequestInfo interface, which enables a
 * resource adapter to pass its own request-specific data structure across the
 * connection request flow.
 */
public class Db4oConnectionRequestInfo implements ConnectionRequestInfo {

	private String serverName;
	private Integer portNumber;
	private String username;
	private String password;

	public Db4oConnectionRequestInfo(String serverName, Integer portNumber,
			String username, String password) {

		this.serverName = serverName;
		this.portNumber = portNumber;
		this.username = username;
		this.password = password;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public Integer getPortNumber() {
		return portNumber;
	}

	public void setPortNumber(Integer portNumber) {
		this.portNumber = portNumber;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * Checks whether this instance is equal to another.
	 * 
	 * @param obj
	 *            other object
	 * 
	 * @return true if the two instances are equal, false otherwise
	 */
	@Override
	public boolean equals(Object obj) {

		boolean equal = false;

		if (obj != null) {
			if (obj instanceof Db4oConnectionRequestInfo) {
				Db4oConnectionRequestInfo other = (Db4oConnectionRequestInfo) obj;

				equal = (this.username).equals(other.username)
						&& (this.password).equals(other.password)
						&& (this.serverName).equals(other.serverName)
						&& (this.portNumber).equals(other.portNumber);
			}
		}
		return equal;
	}

	/**
	 * Returns the hashCode of the ConnectionRequestInfoImpl.
	 * 
	 * @return the hash code of this instance
	 */
	@Override
	public int hashCode() {

		int hashcode = new String("").hashCode();

		if (username != null)
			hashcode += username.hashCode();

		if (password != null)
			hashcode += password.hashCode();

		if (serverName != null)
			hashcode += serverName.hashCode();

		if (portNumber != null)
			hashcode += portNumber.hashCode();

		return hashcode;
	}

}
